﻿// $Id$

namespace Mithril.Server
{
    using System;
    using System.ComponentModel;
    using System.Text;
    using System.Windows.Forms;
    using Mithril.Server.Properties;
    
    public partial class ControlForm : Form
    {
        private const string revision = "$Revision$";

        public ControlForm()
        {
            this.InitializeComponent();

            this.Text += " " + ControlForm.revision;

            this.manager.StartCompleted += new AsyncCompletedEventHandler(manager_StartCompleted);
            this.manager.ProgressChanged += new ProgressChangedEventHandler(manager_ProgressChanged);
        }

        private void manager_StartCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.uxNotify.ShowBalloonTip(3000, "Server Stopped", e.Error == null ? "Requested" : e.Error.Message, ToolTipIcon.Info);
            this.lastException = e.Error;
            this.uxName.Enabled = true;
            this.uxStart.Enabled = true;
            this.uxStop.Enabled = false;
        }

        private void manager_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            buffer.Append(e.UserState.ToString());
            this.uxLog.Text = buffer.ToString();
        }

        private void ControlForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.exiting)
            {
                this.manager.Dispose();
            }
            else
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false;
            }
        }

        private void uxName_Validating(object sender, CancelEventArgs e)
        {
            if (this.uxName.Text == String.Empty)
            {
                this.uxErrorProvider.SetError(this.uxName, "A name is required");
                e.Cancel = true;
            }
        }

        private void uxName_TextChanged(object sender, EventArgs e)
        {
            if (this.uxName.Text != String.Empty)
            {
                this.uxStart.Enabled = true;
                settings.ServerKey = String.Empty;
                settings.Save();
            }
            else
            {
                this.uxStart.Enabled = false;
            }
        }

        private void ControlForm_Load(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(this.uxName.Text))
            {
                this.uxName.Text = Guid.NewGuid().ToString();
            }
            this.uxStart_Click(this, EventArgs.Empty);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.exiting = true;
            this.Close();
        }

        private void showAccountToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(settings.AccountKey, "Mithril Account");
        }

        private void showServicePathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(settings.ServiceBaseAddress, "Mithril Service Address");
        }

        private void uxStart_Click(object sender, EventArgs e)
        {
            this.lastException = null;
            this.uxLog.Clear();

            this.uxStart.Enabled = false;
            this.uxName.Enabled = false;
            this.manager.StartAsync();
            this.uxStop.Enabled = true;
            this.started = DateTime.Now;
        }

        private void uxStop_Click(object sender, EventArgs e)
        {
            this.uxStop.Enabled = false;
            this.manager.StopAsync();
        }

        private void uxNotify_BalloonTipClicked(object sender, EventArgs e)
        {
            if(this.lastException != null)
            {
                MessageBox.Show(this.lastException.ToString(), this.lastException.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void uxDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceProxy proxy = new ServiceProxy();
                proxy.Delete(settings.AccountKey, settings.ServerKey);
                settings.ServerKey = String.Empty;
                settings.Name = String.Empty;
                settings.Save();
                uxName.Text = String.Empty;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.ToString(), x.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void uxStart_EnabledChanged(object sender, EventArgs e)
        {
            if (uxStart.Enabled)
            {
                uxDelete.Enabled = !String.IsNullOrEmpty(settings.ServerKey);
            } else
                uxDelete.Enabled = false;
        }

        private void uxNotify_DoubleClick(object sender, EventArgs e)
        {
            // Set the WindowState to normal if the form is minimized.
            if (this.WindowState == FormWindowState.Minimized)
                this.WindowState = FormWindowState.Normal;

            // Activate the form.
            this.ShowInTaskbar = true;
            this.Activate();
        }

        private bool exiting;
        private Exception lastException;
        private DateTime started = DateTime.MinValue;
        private ServerManager manager = new ServerManager();
        private StringBuilder buffer = new StringBuilder();
        private Settings settings = Settings.Default;

    }
}
